package com.coocaa.internship.controller;

import com.coocaa.internship.common.ResultData;
import com.coocaa.internship.pojo.UserInfo;
import com.coocaa.internship.service.serviceImpl.UserInfoServiceImpl;
import com.coocaa.internship.util.BindingResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * @Description: 用户信息控制层
 * @Author: zwh
 * @Date: 2019-07-27
 * @Time: 21:22
 */
@Slf4j
@Controller
@RequestMapping("/userInfo")
public class UserInfoController {

    @Autowired
    UserInfoServiceImpl userInfoService;

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public ResultData saveUserInfo(@Valid UserInfo userInfo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            BindingResultUtil.resolveErrors(bindingResult);
        }
        return userInfoService.saveUserInfo(userInfo);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResultData updateUserInfo(UserInfo userInfo) {
        return userInfoService.updateUserInfoById(userInfo);
    }

    @RequestMapping(value = "/select", method = RequestMethod.GET)
    @ResponseBody
    public ResultData searchUserInfoById(@RequestParam(value = "userId") Long userId) {
        return userInfoService.searchUserInfoById(userId);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public ResultData deleteUserInfoById(@RequestParam("userId") Long userId) {
        return userInfoService.deleteUserInfoById(userId);
    }

}
